define(function(require, exports, module){
	var api = require("api");

	var EventProxy = require("eventproxy");

	function MyButton(obj) {
		EventProxy.call(this);

		this.obj = obj;
		this.init();
	}

	MyButton.prototype = $.extend(new EventProxy(), {
		init: function(){
			var _this = this;
			this.text = this.obj.text();

			this.obj.on("click", function(){
				_this.timer && clearInterval(_this.timer);
				var count = 0;
				_this.timer = setInterval(function(){
					count = (count+1) % 3;
					_this.obj.text(_this.text + ".".repeat(count));
				}, 300);

				_this.emit("click");
			});
		},
		reset: function(){
			this.timer && clearInterval(this.timer);
			this.obj.text(this.text);
		}
	});

	function User() {
		this.init();
	}

	User.prototype = {
		init: function(){
			this.flag = 0;
			this.outerContainer = $("<div class='user-login-container bookmark-my' style='display: none;'><div id='inner-container' class='inner-container'></div></div>");
			this.outerContainer.appendTo("body");
			this.container = this.outerContainer.find("#inner-container");
		},
		show: function(callback){
			var _this = this;

			var html = [
				"<div class='form'>",
				"<form>",
				"<div class='form-group'><label>用户名: </label><input type='text' class='form-control' id='username'></div>",
				"<div class='form-group'><label>密码: </label><input type='password' class='form-control' id='password'></div>",
				"<div class='form-group clearfix'><button class='btn btn-primary' type='button' id='login'>登录</button><a href='javascript:;' id='register' class='register-link'>注册账户</a></div>",
				"</form>",
				"</div>"
			].join("");

			this.container.empty();
			this.container.html(html);
			this.outerContainer.show();

			this.container.find("#register").on("click", function(){
				_this.showAnimate(_this.showRegister, callback);
			});

			var button =new MyButton(this.container.find("#login"));

			button.on("click", function(){
				var username = _this.container.find("#username").val();
				var password = _this.container.find("#password").val();

				if (username.length == 0 || password.length == 0) {
					button.reset();
					alert('请输入用户名和密码');
					return ;
				}

				_this.doLogin(username, password, function(data, error){
					button.reset();
					!error && callback && callback(data);
				});
			});
		},
		doLogin: function(username, password, callback){
			var _this = this;
			api.bareCall("user/login", {username: username, password: password}, function(json, error){
				
				if (error) {
					alert(error);
					return ;
				}

				if (json.errors) {
					alert('用户名或密码错误');
					return ;
				}

				callback && callback(json.data);

				_this.outerContainer.hide();
			});
		},
		showAnimate: function(next, callback){
			var _this = this;

			if (_this.flag == 0) {
				this.container.css("transform", "rotateY(180deg)");	
			} else {
				this.container.css("transform", "rotateY(0deg)");	
			}

			_this.flag = (_this.flag + 1) % 2;
			
			setTimeout(function(){
				next.call(_this, callback);
				_this.container.find(".form").css("transform", _this.flag == 1 ? "rotateY(-180deg)" : null);
			}, 180);
		},
		showRegister: function(callback){
			var _this = this;
			var html = [
				"<div class='form'>",
				"<form>",
				"<div class='form-group'><label>用户名: </label><input type='text' class='form-control' id='r-username'></div>",
				"<div class='form-group'><label>密码: </label><input type='password' class='form-control' id='r-password'></div>",
				"<div class='form-group'><label>密码: </label><input type='password' class='form-control' id='r-password-confirm'></div>",
				"<div class='form-group clearfix'><button class='btn btn-primary' type='button' id='r-register'>注册</button><a href='javascript:;' id='r-login' class='register-link'>直接登录</a></div>",
				"</form>",
				"</div>"
			].join("");

			this.container.empty();
			this.container.html(html).show();

			this.container.find("#r-login").on("click", function(){
				_this.showAnimate(_this.show, callback);
			});

			var button = new MyButton(this.container.find("#r-register"));
			button.on("click", function(){

				var username = _this.container.find("#r-username").val();
				var password = _this.container.find("#r-password").val();
				var passwordConfirm = _this.container.find("#r-password-confirm").val();

				if (username.length == 0 || password.length == 0) {
					button.reset();
					alert('请输入用户名和密码');
					return ;
				}

				if (password != passwordConfirm) {
					button.reset();
					alert('两次密码输入不一致');
					return ;
				}

				api.bareCall("user/create", {username: username, password: password}, function(json, error){
					if (error) {
						button.reset();
						alert(error);
						return ;
					}

					if (json.errors) {
						button.reset();
						alert('注册失败');
						return ;
					}

					_this.doLogin(username, password, function(data, error){
						button.reset();
						!error && callback && callback(data);
					});
				});
			});
		}
	};

	return User;
});